package com.newtouch.xcd.admin.wechat.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.newtouch.xcd.admin.wechat.builder.TextBuilder;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class SubscribeHandler extends AbstractHandler {

//    @Reference
//    private CwrWechatUserService cwrWechatUserService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) throws WxErrorException {

        this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());

//        // 获取微信用户基本信息
//        try {
//            WxMpUser userWxInfo = weixinService.getUserService()
//                .userInfo(wxMessage.getFromUser(), null);
//
//            if (userWxInfo != null) {
//
//                String openId = userWxInfo.getOpenId();
//                String appId = this.cwrWechatUserService.queryAppId();
//
//                CwrWechatUser cwrWechatUser = cwrWechatUserService.queryByOpenIdAndAppId(openId,appId);
//
//                if(null != cwrWechatUser){
//
//                    //存在了，则更新用户的状态为关注
//                    cwrWechatUser.setSubscribe(userWxInfo.getSubscribe().toString());
//                    cwrWechatUser.setUpdatedTime(DateUtils.now());
//                    this.cwrWechatUserService.saveOrUpdate(cwrWechatUser);
//                }else{
//
//                    //可以添加关注用户到本地数据库
//                    cwrWechatUser = new CwrWechatUser();
//                    cwrWechatUser.setAppId(appId);
//                    cwrWechatUser.setSubscribe(userWxInfo.getSubscribe().toString());
//                    cwrWechatUser.setOpenId(userWxInfo.getOpenId());
//                    cwrWechatUser.setNickname(userWxInfo.getNickname());
//                    cwrWechatUser.setHeadImgUrl(userWxInfo.getHeadImgUrl());
//                    cwrWechatUser.setSubscribeTime(DateUtils.now());
//                    cwrWechatUser.setCreateTime(DateUtils.now());
//                    cwrWechatUser.setUserCode(CwrDict.UserCode.AUTO.getCode());
//                    cwrWechatUser.setDataLevel(CwrDict.DataLevel.D0.getCode());
//                    cwrWechatUser.setVersion(1);
//
//                    cwrWechatUserService.save(cwrWechatUser);
//
//                }
//            }
//        } catch (WxErrorException e) {
//            if (e.getError().getErrorCode() == 48001) {
//                this.logger.info("该公众号没有获取用户信息权限！");
//            }
//        }

        WxMpXmlOutMessage responseResult = null;
        try {
            responseResult = this.handleSpecial(wxMessage);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }

        if (responseResult != null) {
            return responseResult;
        }

        try {
            return new TextBuilder().build("感谢关注", wxMessage, weixinService);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }

        return null;
    }

    /**
     * 处理特殊请求，比如如果是扫码进来的，可以做相应处理
     */
    private WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage)
        throws Exception {
        //TODO
        return null;
    }

}
